home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / UUPC3 / MAC_SPEC / LIB.C < prev    next >
Text File  |  1991-12-02  |  4KB  |  226 lines

  1. /*         lib.c
  2.  
  3. */
  4.  
  5. #ifdef THINK_C
  6. # include "unixlibproto.h"
  7. #endif THINK_C
  8.  
  9. #include <stdio.h>
  10. #include <ctype.h>
  11. #include <errno.h>
  12. #include "host.h"
  13. #ifdef     THINK_C
  14. # include <unix.h>
  15. # include <fcntl.h>
  16. #endif
  17.  
  18. #ifndef NULL
  19. #define NULL 0L
  20. #endif
  21.  
  22. MKDIR( path )
  23. char * path;
  24. {
  25.     char * cp = path;
  26.     char *colon, *slash, *sep, sepchar;
  27.     int first = 1;
  28.  
  29.     if ( *cp == '\0' )
  30.         return( 0 );
  31.  
  32.     /* see if we need to make any intermediate directories */
  33.     while (cp && *cp) {
  34.         slash = index (cp, SEPCHAR);
  35.         colon = index (cp, DIRCHAR);
  36.         sep = slash;
  37.         if (colon && (!slash || colon < slash)) {
  38.             sep = colon;
  39.         }
  40.         if (sep) {
  41.             sepchar = *sep;
  42.             if (sepchar != DIRCHAR || !first /* || sep != path */) {
  43.                 *sep = '\0';
  44.                 mkdir( path );
  45.                 *sep = sepchar;
  46.             }
  47.             sep++;
  48.             first = 0;
  49.         }
  50.         cp = sep;
  51.     }
  52.  
  53.     /* make last dir */
  54.     return( mkdir( path ) );
  55.  
  56. }
  57.  
  58. CHDIR( path )
  59. char * path;
  60. {
  61.     char * cp = path;
  62.  
  63.     if ( *cp == '\0' )
  64.         return( 0 );
  65.  
  66.     MKDIR( path );
  67.  
  68.     /* change to last directory */
  69.     return( chdir( path ) );
  70.  
  71. }
  72.  
  73. int OPEN (name, mode)
  74. char * name;
  75. int mode;
  76. {
  77.     char   nname[255];
  78.     int        results;
  79.     cnvMac(name, nname);
  80.     results = open( nname, mode );
  81.     return results;
  82. }
  83.  
  84. FILE * FOPEN( name, mode, ftype )
  85. char * name;
  86. char * mode;
  87. char ftype;
  88. {
  89.  
  90.     char * last;
  91.     FILE * results;
  92.     char   nname[255];
  93.     char   opmode[5];
  94.     int       len;
  95.  
  96.     /* are we opening for write or append */
  97.  
  98.     FILEMODE( ftype );
  99.     strcpy(opmode, mode);
  100.     if (ftype == 'b') {
  101.         len = strlen(opmode);
  102.         *(opmode+len) = ftype;
  103.         *(opmode+len+1) = '\0';
  104.     }
  105. #if 0
  106.     DebugStr("\pIn FOPEN");
  107. #endif
  108.     cnvMac(name, nname);
  109.     results = fopen( nname, opmode );
  110.  
  111.     if ( results != (FILE *) NULL ) {        /* fixed [garym 3/21/90 */
  112.         /* success, if not reading, set file info */
  113.         if (*mode != 'r') {
  114.             FInfo    fInfo;
  115.             CtoPstr(nname);
  116.             if (GetFInfo((StringPtr)nname, 0, &fInfo) == noErr) {
  117.                 fInfo.fdCreator = 'MPS ';
  118.                 (void)SetFInfo((StringPtr)nname, 0, &fInfo);
  119.             }
  120.         }
  121.         return( results );
  122.     }
  123.     /* are we opening in sub-directory */
  124.     last = rindex( name, SEPCHAR );
  125.  
  126.     /* lets just verify that all sub-dir's exist */
  127.     if ( last != (char *) NULL ) {
  128.         *last = '\0';
  129.         MKDIR( name );
  130.         *last = '/';
  131.     }
  132.  
  133.     /* now try open again */
  134.     return( fopen( nname, opmode ));
  135.  
  136. }
  137.  
  138. int CREAT( name, mode, ftyp )
  139. char * name;
  140. int mode;
  141. char ftyp;
  142. {
  143.  
  144.     char *    last;
  145.     int     results;
  146.     char    nname[255];
  147.     FILE *fp;
  148.     
  149.     /* are we opening for write or append */
  150.     FILEMODE( ftyp );
  151.     cnvMac(name, nname);
  152.     mode = O_CREAT|O_TRUNC|O_WRONLY;    /* ignore Unix perms */
  153.     if (ftyp == 'b') mode |= O_BINARY;
  154.     else mode |= O_TEXT;    
  155.     results = creat( nname, mode );
  156.  
  157.     if ( results != -1 )
  158.         return( results );    /* success */
  159.  
  160.     /* are we opening in sub-directory */
  161.     last = rindex( name, '/' );
  162.  
  163.     /* lets just verify that all sub-dir's exist */
  164.     if ( last != (char *) NULL ) {
  165.         *last = '\0';
  166.         MKDIR( name );
  167.         *last = '/';
  168.     }
  169.  
  170.     /* now try open again */
  171.     results = creat( nname, mode );
  172.     if ( results != -1 )
  173.         return( results );    /* success */
  174.         
  175.     /* maybe it already exists, see if we can open it */
  176.     fp = FOPEN( nname, "w", ftyp );
  177.     if ( fp != NULL ) {
  178.         fclose(fp);
  179.         results = 0;        /* it already exists, no problem */
  180.     } else {
  181.         results = -1;
  182.     }
  183.         
  184.     return( results );
  185.  
  186. }
  187.  
  188. int    UNLINK(path)
  189. char    *path;
  190. {
  191.     char    mpath[255];
  192.     cnvMac(path, mpath);
  193.     return(unlink(mpath));
  194. }
  195.  
  196.  
  197. extern int debuglevel;
  198. extern int remote;
  199.  
  200. #define MASTER 1
  201.  
  202.  
  203. int getargs( line, flds )
  204. char *line;
  205. char **flds;
  206. {
  207.     int i = 0;
  208.     char *s;
  209.  
  210.     while ( (*line != '\0') && (*line != '\n') )
  211.     {
  212.        if ( isspace(*line) )
  213.        {
  214.           line++;
  215.           continue;
  216.        }
  217.        *flds++ = line;
  218.        i++;
  219.        while( (isspace(*line) == 0) && (*line != '\0') ) line++;
  220.        if (isspace(*line)) *line++ = '\0';
  221.     }
  222.     return(i);
  223. }
  224.  
  225.  
  226.